// pages/counselor/chengji/chengji.js
Page({
  data: {
    currentSemester: '2023-2024学年第一学期',
    semesterList: ['2023-2024学年第一学期', '2022-2023学年第二学期', '2022-2023学年第一学期'],
    semesterIndex: 0,
    classList: [
      { id: 1, name: '2023级计算机科学1班' },
      { id: 2, name: '2023级软件工程1班' },
      { id: 3, name: '2022级数据科学1班' }
    ],
    classIndex: 0,
    gradeStats: {
      averageScore: 0,
      passRate: 0,
      excellentRate: 0,
      failCount: 0
    },
    courseList: [],
    isLoading: false
  },

  onLoad: function() {
    this.fetchCourseList();
  },
  
  onShow: function() {
    // 页面显示时刷新数据
    this.fetchCourseList();
  },
  
  // 学期选择
  semesterChange: function(e) {
    this.setData({
      semesterIndex: e.detail.value,
      currentSemester: this.data.semesterList[e.detail.value]
    });
    
    this.fetchCourseList();
  },
  
  // 班级选择
  classChange: function(e) {
    this.setData({
      classIndex: e.detail.value
    });
    
    this.fetchCourseList();
  },
  
  // 获取课程列表
  fetchCourseList: function() {
    this.setData({ isLoading: true });
    
    const classId = this.data.classList[this.data.classIndex].id;
    const semester = this.data.currentSemester;
    
    wx.showLoading({
      title: '加载中...',
    });
    
    wx.request({
      url: 'http://localhost:3000/api/grades/counselor/courses',
      method: 'GET',
      data: {
        classId: classId,
        semester: semester
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        wx.hideLoading();
        
        if (res.statusCode === 200) {
          this.setData({
            courseList: res.data.courses,
            gradeStats: res.data.stats,
            isLoading: false
          });
        } else {
          this.setData({ isLoading: false });
          wx.showToast({
            title: res.data.message || '获取课程数据失败',
            icon: 'none'
          });
          
          // 如果API请求失败，使用模拟数据
          this.useMockData();
        }
      },
      fail: () => {
        wx.hideLoading();
        this.setData({ isLoading: false });
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
        
        // 如果网络请求失败，使用模拟数据
        this.useMockData();
      },
      complete: () => {
        wx.stopPullDownRefresh();
      }
    });
  },
  
  // 使用模拟数据的方法
  useMockData: function() {
    // 使用模拟数据
    const mockCourses = [
      { 
        id: 1, 
        name: '高等数学', 
        teacher: '张教授', 
        credit: 4, 
        studentCount: 45,
        averageScore: 82.5,
        passRate: 95.6,
        excellentRate: 40.0,
        failCount: 2,
        status: '已结课'
      },
      { 
        id: 2, 
        name: '大学英语', 
        teacher: '李教授', 
        credit: 3, 
        studentCount: 45,
        averageScore: 88.2,
        passRate: 100,
        excellentRate: 55.6,
        failCount: 0,
        status: '已结课'
      },
      { 
        id: 3, 
        name: '数据结构', 
        teacher: '王教授', 
        credit: 4, 
        studentCount: 45,
        averageScore: 79.8,
        passRate: 93.3,
        excellentRate: 35.6,
        failCount: 3,
        status: '已结课'
      },
      { 
        id: 4, 
        name: '计算机网络', 
        teacher: '赵教授', 
        credit: 3, 
        studentCount: 45,
        averageScore: 84.5,
        passRate: 97.8,
        excellentRate: 42.2,
        failCount: 1,
        status: '进行中'
      },
      { 
        id: 5, 
        name: '操作系统', 
        teacher: '钱教授', 
        credit: 4, 
        studentCount: 45,
        averageScore: 81.2,
        passRate: 94.5,
        excellentRate: 38.9,
        failCount: 2,
        status: '进行中'
      },
      { 
        id: 6, 
        name: '软件工程', 
        teacher: '孙教授', 
        credit: 3, 
        studentCount: 45,
        averageScore: 86.7,
        passRate: 98.9,
        excellentRate: 46.7,
        failCount: 0,
        status: '未开始'
      }
    ];
    
    // 更新数据
    this.setData({
      courseList: mockCourses,
      gradeStats: {
        averageScore: 85.6,
        passRate: 96.5,
        excellentRate: 45.2,
        failCount: 3
      },
      isLoading: false
    });
  },
  
  // 查看课程详情
  viewCourseDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/counselor/chengji/detail/detail?id=${id}&classId=${this.data.classList[this.data.classIndex].id}&semester=${this.data.currentSemester}`
    });
  },
  
  // 导出成绩
  exportGrades: function() {
    wx.showLoading({
      title: '导出中...',
    });
    
    const classId = this.data.classList[this.data.classIndex].id;
    const semester = this.data.currentSemester;
    
    wx.request({
      url: 'http://localhost:3000/api/counselor/grades/export',
      method: 'POST',
      data: {
        classId: classId,
        semester: semester
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        wx.hideLoading();
        
        if (res.statusCode === 200) {
          wx.showModal({
            title: '导出成功',
            content: '成绩单已导出，请到系统后台下载',
            showCancel: false
          });
        } else {
          wx.showToast({
            title: res.data.message || '导出失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        
        // 模拟成功
        wx.showModal({
          title: '导出成功',
          content: '成绩单已导出，请到系统后台下载',
          showCancel: false
        });
      }
    });
  },
  
  // 下拉刷新
  onPullDownRefresh: function() {
    this.fetchCourseList();
  }
})